package org.caiotte.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.caiotte.domain.Employee;
import org.caiotte.query.EmployeeQuery;
import org.caiotte.service.IEmployeeService;
import org.caiotte.util.AjaxResult;
import org.caiotte.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *  员工管理接口
 *    写接口三要素：
 *      1.请求地址是什么
 *      2.请求参数是什么
 *      3.返回值是什么
 */

//@GetMapping("getById") = @RequestMapping(value="/getById",method = RequestMethod.GET)
//@PostMapping
//@PutMapping
//@PatchMapping
//@DeleteMapping
@RestController   //=  @Controller + @ResponseBody
@RequestMapping("/employee")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;

    //入门样例
    //获取一个员工 http://localhost:8080/dept/getById?id=1  传统写法
     //URL http://localhost:8080/dept/1
    // GET
    /*@GetMapping("/{id}")//  http://localhost:8080/dept/7/sadfasd
    public Employee getById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }*/

    // 查询一条记录  GET
    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Employee queryById(@PathVariable("id") Long id){
        return employeeService.queryById(id);
    }

    //查询所有      GET  http://localhost:8080/dept
    @GetMapping
    public List<Employee> queryAll(){
        return employeeService.queryAll();
    }

    //高级查询 + 分页   POST   http://localhost:8080/dept
    // @RequestBody 从请求体中获取参数
    @PostMapping
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery query){
        return employeeService.queryPage(query);
    }


    //新增 或 修改   PUT
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Employee employee){
        try {
            if(employee.getId() == null){
                employeeService.save(employee);
            }else{
                employeeService.update(employee);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统瓦了，我们正在殴打程序员！！");
        }
    }

    //删除一条记录    DELETE
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        /*  low
        try {
            employeeService.removeById(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            AjaxResult result = new AjaxResult();
            result.setSuccess(false);
            result.setMessage("噢哟，系统瓦了，我们正在殴打程序员！！");
            return result;
        }*/
        try {
            employeeService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统瓦了，我们正在殴打程序员！！");
        }
    }

    /**
     * 批量删除接口
     *   如果前端传递的是数组，那么后台可以使用list  或者 数组接收
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            employeeService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("噢哟，系统瓦了，我们正在殴打程序员！！");
        }
    }


}
